$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        form:null,
        dataGrid:null,
        frame:null,
        eastPanel:null,
        eastPanelStatus:'close',
        init:function () {
            this.dataGrid = new xhDataGrid($('#list'),'feedback/getFeedback',{});
            this.dataGrid.parseToolbar().init().activeButton(['bt_list_del','bt_list_apply'],['bt_list_shield']);
            this.frame = $('[name=detailFrame]');
            this.eastPanel = $('#eastPanel');
            this.listener();
            this.funObj().bindDgEvent();
        },
        listener:function () {
            var $this = this;

            //用户类型选中操作
            $("#select_userType").combobox({
                onSelect:function (row) {
                    $this.funObj().triggerUserType(row.value);
                }
            });

            //学校文字过滤
            $('#select_schools').combobox({
                editable:true,
                filter: function(q, row){
                    var opts = $(this).combobox('options');
                    return row[opts.textField].indexOf(q) == 0;
                }
            });

            //区域选中
            $('#select_area').combobox({
                onChange:function () {
                    $this.funObj().getCity($('#select_area').combobox('getValue'));
                    $this.funObj().filterSchool();
                }
            });
            //市选中
            $("#select_city").combobox({
                onChange:function () {
                    $this.funObj().filterSchool();
                }
            });

            //搜索
            $('#bt_list_search').click(function () {
                $this.funObj().doSearch();
            });

            //回复
            $("#bt_list_apply").click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().openApplyForm();
            });
            //屏蔽
            $("#bt_list_shield").click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().openShieldForm();
            });
            //删除
            $("#bt_list_del").click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().askDelQuestion();
            });
            //查看回复设置
            $("#bt_list_seeTemplate").click(function () {
                $this.funObj().openTemplate();
            });
            //查看禁言列表
            $("#bt_list_seeShieldList").click(function () {
                $this.funObj().openShield();
            });


        },
        funObj:function () {
            var $this = this;
            return {
                getCity:function ($id) {
                    $("#select_city").combobox({
                        url:baseUrl+'feedback/getCity',
                        valueField:'id',
                        textField:'text',
                        onBeforeLoad:function (param) {
                            param.id = $id;
                        }
                    });
                },
                filterSchool:function () {
                    var $area =  $('#select_area').combobox('getValue');
                    var $city =  $('#select_city').combobox('getValue');
                    $("#select_schools").combobox({
                        url:baseUrl+'feedback/filterSchool',
                        valueField:'id',
                        textField:'text',
                        onBeforeLoad:function (param) {
                            param.area = $area;
                            param.city = $city;
                        }
                    });
                },
                triggerUserType:function(value){
                    $("#select_packageName").combobox({
                        url:baseUrl+'feedback/ajaxGetApp',
                        valueField:'id',
                        textField:'text',
                        onBeforeLoad:function (param) {
                            param.userType = value;
                        }
                    });
                },
                doSearch:function () {
                    $this.dataGrid.reload({
                        schoolId: xhExtra.getValue($('#select_schools')),
                        userType: xhExtra.getValue($('#select_userType')),
                        packageName: xhExtra.getValue($('#select_packageName')),
                        operate: xhExtra.getValue($('#select_status')),
                        key: xhExtra.getValue($('#list_search')),
                        searchType: xhExtra.getValue($('#list_search_type')),
                        startTime: xhExtra.getValue($('#search_startTime')),
                        endTime: xhExtra.getValue($('#search_endTime')),
                        area: xhExtra.getValue($('#select_area')),
                        city: xhExtra.getValue($('#select_city'))
                    });
                },
                openApplyForm:function () {
                    var countNum = $this.dataGrid.command('getSelections').length;
                    if(countNum == 1) {
                        var name = $this.dataGrid.getSelectValue('userName');
                        var id = $this.dataGrid.getSelectValue('id');
                        var describe = $this.dataGrid.getSelectValue('describe');
                        window.top.xhTop.showModal({
                            title: '回复 '+name,
                            content: '<iframe  src="'+baseUrl+'feedback/replyForm?needReload=true&id='+id+'&describe='+encodeURI(describe)+'"  style="width: 100%;;border-style: none;padding: 0px;height:300px"  scrolling="yes"   ></iframe>',
                            width:900,
                            button:'确定',
                            backClose:false
                        });
                    } else if (countNum > 10){
                        window.top.xhTop.showError('错误', '批量回复请不要超过10个');
                    } else {
                        var names = $this.dataGrid.getSelectValues('userName');
                        var ids = $this.dataGrid.getSelectValues('id');
                        names = names.join(',') + ' 等';
                        ids = ids.join(',');
                        window.top.xhTop.showModal({
                            title: '回复 '+names,
                            content: '<iframe  src="'+baseUrl+'feedback/replyForm?needReload=true&id='+ids+'"  style="width: 100%;;border-style: none;padding: 0px;height:300px"  scrolling="yes"   ></iframe>',
                            width:900,
                            button:'确定',
                            backClose:false
                        });
                    }
                },
                openShieldForm:function () {
                    var name = $this.dataGrid.getSelectValue('userName');
                    var id = $this.dataGrid.getSelectValue('id');
                    window.top.xhTop.showModal({
                        title: '禁言 '+name,
                        content: '<iframe  src="'+baseUrl+'feedback/shieldForm?needReload=true&id='+id+'"  style="width: 100%;;border-style: none;padding: 0px;height:200px"  scrolling="yes"   ></iframe>',
                        width:900,
                        button:'确定',
                        backClose:false
                    });
                },
                askDelQuestion:function () {
                    var ids = $this.dataGrid.getSelectValues('id');
                    xhExtra.askChangeStatus('feedback/deleteFeedBack',{ids:ids},$this.dataGrid,'删除后，客户端也会同步删除',function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                    })
                },
                openTemplate:function () {
                    window.top.xhTop.createNewTab('回复设置',baseUrl+'feedback/templateList');
                },
                openShield:function () {
                    window.top.xhTop.createNewTab('禁言列表',baseUrl+'feedback/shieldList');
                },
                bindDgEvent:function () {
                    $this.dataGrid.rowStyler = function(rowIndex, rowData){
                        if(rowData.forbidStatus ==  1){
                            return 'background-color: yellow';
                        }
                    };
                    $this.dataGrid.onDblClickRow = function (rowIndex,rowData) {
                        $('#targetTr').show();
                        $('#targetTr').prev().hide();
                        $('#mainLayout').layout('collapse', 'east');
                        $this.frame.attr('src',baseUrl+'feedback/feedbackDetail?id='+rowData['id']);
                    };
                    $this.dataGrid.onClickRow = function (rowIndex,rowData) {  //打开状态下 单机触发加载数据
                        if ($this.eastPanelStatus == 'close') {
                            var p = $("#mainLayout").layout("panel", "east")[0].clientWidth;
                            if (p <= 0) {
                                $('#mainLayout').layout('expand','east');
                            }
                        }
                        $this.eastPanel.panel('setTitle', '回复('+ rowData['userName']+')');
                        $('[name=replyFrame]')[0].contentWindow.location = baseUrl+'feedback/replyPanel?id='+rowData['id'];
                    };

                    $this.eastPanel.panel({
                        collapsed:true, //折叠
                        onCollapse:function () {
                            $this.eastPanelStatus = 'close';
                            $this.funObj().emptyEastPanel();
                        },
                        onExpand:function () {
                            $this.eastPanelStatus = 'open';
                        }
                    });
                },
                backToList:function () {
                    $('#targetTr').hide();
                    $('#targetTr').prev().show();
                    $("#mainLayout").layout("resize");//重绘
                    $this.dataGrid.reload();
                },
                getNextFrontId:function(){
                    var obj = {};
                    var arr = [];
                    for (var i= 0 ; i< $('[field=id]').length;i++){
                        var id = $('[field=id]').eq(i).text();
                        if (id && id != 'id') {
                            arr.push(id);
                        }
                    }
                    for (var $i = 0; $i < arr.length; $i++) {
                        var $prefIndex = $i - 1;
                        var nextIndex  = $i + 1;
                        var item = {};
                        if ($prefIndex >= 0) {
                            item.front = arr[$prefIndex];
                        }
                        if (arr[nextIndex]) {
                            item.next = arr[nextIndex];
                        }
                        obj[arr[$i]] = item;
                    }
                    return obj;
                },
                emptyEastPanel:function () {
                    $('[name=replyFrame]')[0].contentWindow.location = baseUrl+'feedback/replyChoose';
                    $this.eastPanel.panel('setTitle', '回复面板');
                },
                successEastPanel:function () {
                    $('[name=replyFrame]')[0].contentWindow.location = baseUrl+'feedback/replySuccess';
                    $this.eastPanel.panel('setTitle', '回复成功');
                }
            }
        }
    }
};